import 'package:acgn_pioneer/models/comment_model.dart';

import '../../../public.dart';

/// 评论服务类
///
class ReaderCommentService {
  ReaderCommentService();

  /// 获取热门评论
  Future<List<CommentModel>> getHotComment(int page, int comicId, int chapterId,
      {int pageSize = 20, int fatherid = 0}) async {
    List<CommentModel> re = await ACGN.http.fetch<CommentModel>(
        ACGN.config.interfaceApi.comtGethotscomment,
        queryParameters: {
          'appid': 24,
          'ssid': comicId,
          'relateid': chapterId,
          'ssidtype': 0, // 主体标识：（必传） 0：漫画 1：社区 2：游戏 3：书单 7：动态漫  8：相关视频
          'contenttype':
              3, // 0: 热门评论（没有数据时返回最新评论） 1：加精，置顶的热门评论  2：普通热门评论  3：所有热门评论
          'page': page,
          'size': pageSize,
          'fatherid': fatherid // 父级id
        });
    return re;
  }

  /// 获取评论总数量
  Future<int> getNewCommentCount(int comicId, int chapterId) async {
    var re = await ACGN.http.fetch(ACGN.config.interfaceApi.newcommentcount,
        queryParameters: {
          'appid': 24,
          'ssid': comicId,
          'ssidtype': 0,
          'commentType': 2,
          'relateId': chapterId
        },
        isParse: false);
    return re == null ? 0 : re;
  }

  /// 获取子评论列表
  Future<List<CommentModel>> getChildrenComment(String ids) async {
    List<CommentModel> re = await ACGN.http.fetch<CommentModel>(
        ACGN.config.interfaceApi.getchildrencomment,
        queryParameters: {'comment_ids': ids});
    return re;
  }

  /// 获取普通评论
  Future<List<CommentModel>> getNormalComment(
      int page, int comicId, int chapterId,
      {bool isWater = true, int pageSize = 20, int fatherid = 0}) async {
    List<CommentModel> re = await ACGN.http.fetch<CommentModel>(
        ACGN.config.interfaceApi.newgetsv2,
        queryParameters: {
          'appid': 24,
          'ssid': comicId,
          'relateid': chapterId,
          'ssidtype': 0,
          'page': page,
          'size': pageSize,
          'fatherid': fatherid,
          'iswater': isWater ? 1 : 0
        });
    return re;
  }

  /// 提交评论
  Future<bool> addComment(CommentModel commentModel) async {
    var re = await ACGN.http.fetch(ACGN.config.interfaceApi.newcommentadd,
        method: HttpMethodType.POST,
        hasStatus: true,
        data: {
          "starId": 0,
          "opreateId": 0,
          "satelliteId": 0,
          "relateId": commentModel.relateId,
          "title": commentModel.title,
          "ssid": commentModel.ssid,
          "content": commentModel.content,
          "appid": 24,
          "fatherId": commentModel.fatherId,
          "ssidType": 0
        });
    var flag = re['status'] == 1;
    ACGN.toast.message(!flag ? re['msg'] : '评论成功');
    return flag;
  }

  /// 提交点赞
  Future<bool> addCommentSupport(CommentModel commentModel, int status) async {
    var re = await ACGN.http.fetch(ACGN.config.interfaceApi.newcommentsupport,
        method: HttpMethodType.PUT,
        hasStatus: true,
        data: {
          "userIdentifier": commentModel.userIdentifier,
          "appId": "24",
          "commentId": commentModel.fatherId,
          "ssidType": 0,
          "ssid": commentModel.ssid,
          "status": status
        });

    var flag = re['status'] == 1;
    return flag;
  }
}
